<?php
namespace app\controller;

use app\facade\Token;
use think\facade\View;
use app\model\score\ScoreModel;

class Score
{
    public function index() {
        $uid = Token::getUid();
        View::assign([
            "uid"   => $uid,
        ]);
        return View::fetch();
    }
    public function get() {
        $uid = Token::getUid();
        return json(ScoreModel::getUserScore($uid));
    }

    public function admin($type="default") {
        View::assign([
            "type"   => $type,
        ]);
        return View::fetch();
    }
    public function admin_getAll() {
        $type = input("get.type");
        if ($type == "") return json([]);
        
        return json(ScoreModel::getAllByType($type));
    }
    public function admin_post() {
        $type = input("post.type");
        $data = input("post.data");
        if ($data == "") return;
        if ($type == "default") return json(["msg"=>"请先新建考试","code"=>1]);
        if (!ScoreModel::typeExist($type)) return json(["msg"=>"请先新建考试","code"=>1]);
        $data = json_decode($data);
        foreach($data as $i) {
            ScoreModel::saveData($type,$i->uid,json_encode($i));
        }
        return json(["code"=>0]);
    }
    public function admin_create() {
        $type = input("post.type");
        if ($type == "") return;
        if (ScoreModel::typeExist($type)) return json(["msg"=>"考试已存在","code"=>1]);
        ScoreModel::saveData($type,0,"");
        return json(["code"=>0]);
    }
}